package com.xiaocai.videotest;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Surface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.Spinner;

import com.xiaocai.glsurface.ColorFilter;
import com.xiaocai.glsurface.DisplayMode;
import com.xiaocai.glsurface.MyGLSurfaceView;
import com.xiaocai.glsurface.ScreenSplit;
import com.xiaocai.glsurface.SurfaceStateCallback;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SurfaceStateCallback, AdapterView.OnItemSelectedListener, SeekBar.OnSeekBarChangeListener, View.OnClickListener {

    private MyGLSurfaceView myGLSurfaceView;

    private MediaPlayer mediaPlayer;

    private Spinner displaySpinner;

    private Spinner colorSpinner;

    private SeekBar red, green, blue;

    private CheckBox mirrorX, mirrorY;

    private Spinner screenSpinner;

    private float[] rgba = new float[4];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        displaySpinner = findViewById(R.id.display_mode);
        colorSpinner = findViewById(R.id.color_filter);
        red = findViewById(R.id.red);
        green = findViewById(R.id.green);
        blue = findViewById(R.id.blue);
        mirrorX = findViewById(R.id.mirror_x);
        mirrorY = findViewById(R.id.mirror_y);
        screenSpinner = findViewById(R.id.screen_splite);

        displaySpinner.setOnItemSelectedListener(this);
        colorSpinner.setOnItemSelectedListener(this);
        screenSpinner.setOnItemSelectedListener(this);
        red.setOnSeekBarChangeListener(this);
        green.setOnSeekBarChangeListener(this);
        blue.setOnSeekBarChangeListener(this);
        mirrorX.setOnClickListener(this);
        mirrorY.setOnClickListener(this);

        myGLSurfaceView = findViewById(R.id.surface);
        myGLSurfaceView.setSurfaceStateCallback(this);

    }

    @Override
    protected void onPause() {
        super.onPause();

        myGLSurfaceView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

        myGLSurfaceView.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (parent == displaySpinner) {
            switch (position) {
                default:
                case 0:
                    myGLSurfaceView.setDisplayMode(DisplayMode.FULL);
                    break;
                case 1:
                    myGLSurfaceView.setDisplayMode(DisplayMode.RATIO);
                    break;
                case 2:
                    myGLSurfaceView.setDisplayMode(DisplayMode.ORIGINAL);
                    break;
                case 3:
                    myGLSurfaceView.setDisplayMode(DisplayMode.FULL_RATIO);
                    break;
            }
        } else if (parent == colorSpinner) {
            switch (position) {
                default:
                case 0:
                    myGLSurfaceView.setColorFilter(ColorFilter.NONE);
                    break;
                case 1:
                    myGLSurfaceView.setColorFilter(ColorFilter.GRAY);
                    break;
                case 2:
                    myGLSurfaceView.setColorFilter(ColorFilter.RGBA);
                    break;
            }
        } else if (parent == screenSpinner) {
            switch (position) {
                default:
                case 0:
                    myGLSurfaceView.setScreenSplit(ScreenSplit.NONE);
                    break;
                case 1:
                    myGLSurfaceView.setScreenSplit(ScreenSplit.DOUBLE);
                    break;
                case 2:
                    myGLSurfaceView.setScreenSplit(ScreenSplit.THREE);
                    break;
                case 3:
                    myGLSurfaceView.setScreenSplit(ScreenSplit.FOUR);
                    break;
                case 4:
                    myGLSurfaceView.setScreenSplit(ScreenSplit.NINE);
                    break;
            }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar == red) {
            rgba[0] = (progress - 100) / 100f;
        } else if (seekBar == green) {
            rgba[1] = (progress - 100) / 100f;
        } else if (seekBar == blue) {
            rgba[2] = (progress - 100) / 100f;
        }
        myGLSurfaceView.setColor(rgba);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onClick(View v) {
        myGLSurfaceView.setMirror(mirrorX.isChecked(), mirrorY.isChecked());
    }

    @Override
    public void create(Surface surface) {
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.setVolume(0, 0);
        mediaPlayer.setLooping(true);
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                int videoWidth = mp.getVideoWidth();
                int videoHeight = mp.getVideoHeight();
                myGLSurfaceView.setVideoSize(videoWidth, videoHeight);

                /*myGLSurfaceView.setScreenSplit(ScreenSplit.NINE);
                myGLSurfaceView.setColor(new float[] {0, 0, -0.3f, -1});
                myGLSurfaceView.setColorFilter(ColorFilter.RGBA);
                myGLSurfaceView.setDisplayMode(DisplayMode.RATIO);
                myGLSurfaceView.setMirror(false, false);*/

                mp.start();
            }
        });
        mediaPlayer.setSurface(surface);
    }

    @Override
    public void destroy() {

        if (mediaPlayer != null) {
            mediaPlayer.setSurface(null);
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}